package com.psp.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.northend.util.NumUtil;
import com.psp.web.controller.res.BaseResult;
import com.psp.web.controller.res.ListResult;
import com.psp.web.controller.res.ObjectResult;
import com.psp.web.controller.res.bean.RInvoiceHeaderDetailBean;
import com.psp.web.controller.res.bean.RInvoiceHeaderListBean;
import com.psp.web.controller.springmvc.req.GetInvoiceHeaderDeleteParam;
import com.psp.web.controller.springmvc.req.GetInvoiceHeaderDetailParam;
import com.psp.web.controller.springmvc.req.GetInvoiceHeaderListParam;
import com.psp.web.controller.springmvc.req.GetInvoiceHeaderSaveParam;
import com.psp.web.service.InvoiceService;
import com.psp.web.service.exception.ServiceException;
import com.psp.web.service.impl.PageResult;

@Component
public class InvoiceController {

	@Autowired
	InvoiceService invoiceServiceImpl;

	/**
	 * 发票头列表
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<RInvoiceHeaderListBean> headerList(GetInvoiceHeaderListParam param, HttpServletRequest request,
			HttpServletResponse response) {
		String token = (String) request.getAttribute("token");
		String uid = (String) request.getAttribute("userId");
		int page = NumUtil.toInt(param.getPage(), 0);
		int pageSize = 10;
		ListResult<RInvoiceHeaderListBean> result = new ListResult<>();
		PageResult<RInvoiceHeaderListBean> resData = invoiceServiceImpl.getHeaderList(page, pageSize, uid);
		if (resData != null) {
			result.setTotalSize(resData.getCount());
			result.setData(resData.getData());
		}
		result.setFlag(true);
		result.setPage(page);
		result.setToken(token);
		return result;
	}

	/**
	 * 发票头保存
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult headerSave(GetInvoiceHeaderSaveParam param, HttpServletRequest request,
			HttpServletResponse response) {
		BaseResult result = new BaseResult();
		String token = (String) request.getAttribute("token");
		try {
			String uid = (String) request.getAttribute("userId");
			String id = param.getId();
			String title = param.getTitle();
			int type = NumUtil.toInt(param.getType(), 1);

			boolean flag = invoiceServiceImpl.saveHeader(id, uid, title, type);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		result.setToken(token);
		return result;
	}

	/**
	 * 发票头删除
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult headerDelete(GetInvoiceHeaderDeleteParam param, HttpServletRequest request,
			HttpServletResponse response) {
		BaseResult result = new BaseResult();
		String token = (String) request.getAttribute("token");
		try {
			String uid = (String) request.getAttribute("userId");
			String id = param.getId();
			boolean flag = invoiceServiceImpl.deleteHeader(uid, id);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		result.setToken(token);
		return result;
	}

	public ObjectResult<RInvoiceHeaderDetailBean> headerDetail(GetInvoiceHeaderDetailParam param,
			HttpServletRequest request, HttpServletResponse response) {
		ObjectResult<RInvoiceHeaderDetailBean> result = new ObjectResult<RInvoiceHeaderDetailBean>();
		String token = (String) request.getAttribute("token");
		String uid = (String) request.getAttribute("userId");
		String id = param.getId();
		RInvoiceHeaderDetailBean rBean = invoiceServiceImpl.getHeaderDetail(id, uid);
		result.setFlag(rBean != null);
		result.setData(rBean);
		result.setToken(token);
		return result;
	}

}
